/*
 * jQuery Tooltip plugin 1.3
 *
 * 2009: Heavily modified and extended by Razorfish (Dennis Hall)
 *
 * http://bassistance.de/jquery-plugins/jquery-plugin-tooltip/
 * http://docs.jquery.com/Plugins/Tooltip
 *
 * Copyright (c) 2006 - 2008 Jörn Zaefferer
 *
 * $Id: jquery.tooltip.js 5741 2008-06-21 15:22:16Z joern.zaefferer $
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */

;(function($) {

		// the tooltip element
	var helper = {},
		// the current tooltipped element
		current,
		// the title of the current element, used for restoring
		title,
		// timeout id for delayed tooltips
		tID,
		// IE 5.5 or 6
		IE = $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent),
		// flag for mouse tracking
		track = false;

	$.tooltip = {
		blocked: false,
		defaults: {
			delay: 200,
			fade: false,
			top: 15,
			left: 15,
			id: 'tooltip',
			bodyHandler:function(){return '';}
		},
		block: function() {
			$.tooltip.blocked = !$.tooltip.blocked;
		}
	};

	$.fn.extend({
		tooltip: function(settings) {
			settings = $.extend({}, $.tooltip.defaults, settings);
			createHelper(settings);

			// [falbar: 5/25/2010] convert to live events
			return $(this)
				.live('mouseover', function(e) {
					if( !$.data(this,'tooltip') ) {  // If tooltip has not been set up [falbar: 5/25/2010]
						$.data(this,'tooltip', settings);
						this.tOpacity = helper.parent.css('opacity');
						// copy tooltip into its own expando and remove the title
						this.tooltipText = this.title;
						$(this).removeAttr('title');
						// also remove alt attribute to prevent default tooltip in IE
						this.alt = '';
						
					}
					save.call(this, e);
				})
				.live('mouseout',hide)
				.live('click',function(){return false;});

		},
		hideWhenEmpty: function() {
			return this.each(function() {
				$(this)[ $(this).html() ? 'show' : 'hide' ]();
			});
		}
	});

	function createHelper(settings) {

		// there can be only one tooltip helper
		if (helper.parent) return;

		// create the helper
		helper.parent = $('<div id="' + settings.id + '">'+(IE?'<iframe style="position:absolute" src="javascript:false;" frameborder="0" scrolling="no"></iframe>':'')+'<div class="tooltip-inner"><div class="tooltip-bg"></div><div class="body"></div><div class="footer-cap"><div/></div></div></div>')

			// add to document
			.appendTo(document.body)
			.mouseout(hide)

			// hide it at first
			.hide();

		if (IE) {
			helper.parent.find('iframe')[0].style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
		}

		// save references to elements
		helper.inner = $('.tooltip-inner', helper.parent);
		helper.body = $('div.body', helper.parent);
		helper.iframe = $('iframe', helper.parent);
	}

	function settings(element) {
		return $.data(element,'tooltip');
	}

	// main event handler to start showing tooltips
	function handle(event) {

		// show helper, either with timeout or on instant
		if (settings(this).delay) tID = setTimeout(show, settings(this).delay);
		else show();

		// if selected, update the helper position when the mouse moves
		track = !!settings(this).track;
		if(track) $(document.body).bind('mousemove',update);

		// update at least once
		update(event);
	}

	// save elements title before the tooltip is displayed
	function save() {

		// if this is the current source, or it has no title (occurs with click event), stop
		if ($.tooltip.blocked || this == current || (!this.tooltipText && !settings(this).bodyHandler)) return;

		// save current
		current = this;
		title = this.tooltipText;

		var bodyContent = settings(this).bodyHandler.call(this);

		if (bodyContent.nodeType || bodyContent.jquery) helper.body.empty().append(bodyContent);
		else helper.body.html(bodyContent);

		handle.apply(this, arguments);
	}

	// delete timeout and show helper
	function show() {
		tID = null;
		if (!IE && settings(current).fade) {
			if (helper.parent.is(':animated')) {
				helper.parent.stop().show().fadeTo(settings(current).fade, current.tOpacity);
			} else {
				helper.parent.is(':visible') ?
					helper.parent.fadeTo(settings(current).fade, current.tOpacity) :
					helper.parent.fadeIn(settings(current).fade);
			}
		} else {
			helper.parent.show();
		}
		update();
	}

	// callback for mousemove
	// updates the helper position
	// removes itself when no current element

	function update(event) {

		if($.tooltip.blocked) return;
		if (event && event.target.tagName == 'OPTION') return;

		// stop updating when tracking is disabled and the tooltip is visible
		if (!track && helper.parent.is(':visible') || !current) {
			$(document.body).unbind('mousemove',update);
		}

		// if no current element is available, remove this listener
		if(!current) return;

		var left = helper.parent[0].offsetLeft;
		var top = helper.parent[0].offsetTop;
		if (event) {
			var offset = $(current).offset();
			left = offset.left;
			top = offset.top;

			helper.parent.css({
				left: left,
				top: top
			});
		}

		// check horizontal position
		var siteContainerLeft = $('.glb-bdy').offset().left;
		var hasImage = helper.body.find('img').size();
		var classnames = 'tooltip-left,tooltip-right,tooltip-image-left,tooltip-image-right,tooltip-pdp-left,tooltip-pdp-right'.split(',');
		var isPDP = $(current).is('.pdp-tooltip');
		var isMyTips = $(current).is('.mytips-tooltip');

		// remove all tooltip-type/positional classnames
		helper.inner.removeClass(classnames.join(' '));

		// add appropriate tooltip-type/positional classname
		var className = isPDP ? 'pdp-' + ((left - 322 < siteContainerLeft) ? 'right':'left')
							  : (hasImage ? 'image-' : '') + ((left - (hasImage ? 332 : 202) < siteContainerLeft) ? 'right':'left');

		if (isPDP) {
			if(className.indexOf('left')){
				className = className.replace('left','right');
				helper.parent.css('margin-left', current.offsetWidth - 3);
			} else {
				className = className.replace('right','left');
			}
		}

		helper.inner.addClass('tooltip-' + className);

		if (!isPDP) helper.parent.css('top', top - helper.inner.height() + (IE?50:40) + (hasImage?10:0) );

		if (isMyTips) helper.parent.css('left',left + 25);

		if(IE) {

			var h = helper.inner[0].offsetHeight;

			for(var i=0;i<classnames.length;i++){
				if(helper.inner.is('.'+classnames[i])){
					helper.iframe.css([
						{width:251,height:h,top:-104,left:-202},	//tooltip-left
						{width:251,height:h,top:-104,left:-29},		//tooltip-right
						{width:381,height:h,top:-124,left:-332},	//tooltip-image-left
						{width:381,height:h,top:-124,left:-29},		//tooltip-image-right
						{width:320,height:h,top:-25,left:0},		//tooltip-pdp-left
						{width:320,height:h,top:-25,left:0}			//tooltip-pdp-right
					][i]);
				}
			}
		}
	}

	// hide helper and restore added classes and the title
	function hide(event) {
		if ($.tooltip.blocked) return;

		// don't hide tooltip if we have moused from the trigger to the tooltip,
		// such as when the tooltip appears between the trigger and the cursor in the z-axis
		var targ = event.relatedTarget;
		while(targ){
			try{
				//ff occasionally chokes on this .. but maybe only if there's a firebug open
				if (targ == helper.parent[0]) return;
				targ = targ.parentNode;
			} catch(e) {
				targ = null;
			}
		}

		// clear timeout if possible
		if (tID) clearTimeout(tID);

		if(current){
			var tsettings = settings(current);
			function complete() {
				helper.parent.hide().css('opacity','');
			}
			if ((!IE || !$.fn.bgiframe) && tsettings.fade) {
				if (helper.parent.is(':animated')){
					helper.parent.stop().fadeTo(tsettings.fade, 0, complete);
				} else {
					helper.parent.stop().fadeOut(tsettings.fade, complete);
				}
			} else {
				complete();
			}
		}

		// no more current element
		current = null;
	}

})(jQuery);

foo.mod.tooltip = {
	init: function(m,scope){
		var scope = (scope) ? scope : jQuery(document);
		foo.log('foo.tooltip.init');
		jQuery(m,scope).tooltip({
			bodyHandler: function() {
				var href = $(this).attr('href');
				var id = href.split('#').pop();
				return $('#'+id).html();
			}
		});
	}
};